<?php

/*
Gestion des categories (créneaux horaires)
*/

function cat_get_by_id($id)
{
	$id = intval($id);
	$sql = 'SELECT name, age_min, age_max, description, max_places
	        FROM Category
	        WHERE id = '.$id.';';
	$data = sql_query($sql);
	return mysql_fetch_assoc($data);
}

/**
 * Obtenir la liste de toutes les categories
 * @return record set
 */
function cat_get_list()
{
	$sql = 'SELECT id, name, age_min, age_max, description, max_places
	        FROM Category
			ORDER BY age_max;';
	$data = sql_query($sql);
	return $data;
}


/**
 * Obtenir la liste de tous les membres pour un couple créneau/gymnase
 * @param id_cat: l'id du créneau (categorie) concerné
 * @param id_gym: l'id du gymnase concerné
 * return record set de members
 */
function cat_get_members($id_cat, $id_gym)
{
	$id_cat = intval($id_cat);
	$id_gym = intval($id_gym);
	$sql = 'SELECT m.id, m.idCategory, m.idGymnasium, m.name, m.firstname, m.date_pre_registration, m.birth, s.code as status_code
	        FROM Member m
			JOIN MemberStatus s ON m.status_id = s.id
			WHERE m.idCategory = '.$id_cat.'
			      AND m.idGymnasium = '.$id_gym.'
			      AND (s.code = "WAITING_MEMBER" OR s.code = "MEMBER")
			ORDER BY m.date_pre_registration;';
	$data = sql_query($sql);
	return $data;
}


/**
 * Obtenir le nom d'un créneau par son id
 */
function cat_get_name($id)
{
	$sql = 'SELECT name
	        FROM Category
			WHERE id = '.$id.';';
	$data = mysql_fetch_assoc(sql_query($sql));
	return $data['name'];
}


function cat_update($id, $name, $age_min, $age_max, $description, $max_places)
{
	$id = intval($id);
	$name = mysql_real_escape_string($name);
	$age_min = intval($age_min);
	$age_max = intval($age_max);
	$description = mysql_real_escape_string($description);
	$max_places = $max_places != 0 ? intval($max_places) : '"NULL"';

	$sql = 'UPDATE Category
	        SET name = "'.$name.'",
	            age_min = '.$age_min.',
	            age_max = '.$age_max.',
	            description = "'.$description.'",
	            max_places = '.$max_places.'
	        WHERE id = '.$id.';';
	return sql_query($sql);
}
